<?php

namespace App\Admin\Extensions;

use Encore\Admin\Admin;

class Editpass
{
    protected $id;

    public function __construct($uid, $title)
    {
        $this->id = $uid;
        $this->title = $title;
    }

    protected function script()
    {
        return <<<SCRIPT

layui.use('layer', function(){
        var layer = layui.layer;
        $('.edit_pass').click(function () {
           var that = this;
           var uid = $(that).attr('data-uid');
           var title = $(that).attr('data-title');
           var html = '';
           html += '<\div style="margin-top: 20px;margin-left: 10px;">';
           html += '&nbsp;新密码：<\input name="pass" style="display:inline-block;width:70%;">';
           html += '</\div>';
           var index = layer.open({
              type: 1, 
              title: title,
              content: html,
              area: ['30%','30%'],
              btn: ['确定', '取消'],
              yes: function(res){
                    var pass = $('input[name=pass]').val();
                    $.ajax({
                        url: '/admin/common/user/editpass',
                        type: 'post',
                        data: {
                            id: uid,
                            pass: pass
                        },
                        success: function(res){
                            layer.msg(res.text);
                            if(res.code == 0){
                               layer.close(index);
                            }
                        }
                    });
              }
           }); 
       });
});

SCRIPT;
    }

    protected function render()
    {
        Admin::script($this->script());
        return "&nbsp;<a href='javascript:void(0);'><button data-uid='{$this->id}'  data-title='{$this->title}' class='edit_pass layui-btn  layui-btn-normal layui-btn-xs'>密</button></a>";
    }

    public function __toString()
    {
        return $this->render();
    }
}